package com.yuetong.commom.controller;

import com.yuetong.business.service.*;
import com.yuetong.commom.domain.CommonQuery;
import com.yuetong.common.core.controller.BaseController;
import com.yuetong.common.core.domain.AjaxResult;
import com.yuetong.quotation.service.IYtQuotationSummaryService;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 通用请求
 * 
 * @author  卓晓君
 * @date 2025-03-07
 */
@RestController
@RequestMapping("/common")
public class CommomController extends BaseController
{
    //电梯总数
    private final IYtElevatorInfoService elevatorInfoService;
    private final IYtElevatorBrandsService elevatorBrandsService;
    private final IYtQuotationSummaryService quotationSummaryService;

    private final IYtMaintenanceRecordService ytMaintenanceRecordService;

    private  final IYtPlaceInfoService ytPlaceInfoService;
    private  final IYtContractInfoService ytContractInfoService;

    private   final  IYtPartnerService ytPartnerService;
    public CommomController(IYtElevatorInfoService elevatorInfoService, IYtElevatorBrandsService elevatorBrandsService, IYtQuotationSummaryService quotationSummaryService, IYtMaintenanceRecordService ytMaintenanceRecordService, IYtPlaceInfoService ytPlaceInfoService, IYtContractInfoService ytContractInfoService, IYtPartnerService ytPartnerService) {
        this.elevatorInfoService = elevatorInfoService;
        this.elevatorBrandsService = elevatorBrandsService;
        this.quotationSummaryService = quotationSummaryService;
        this.ytMaintenanceRecordService = ytMaintenanceRecordService;
        this.ytPlaceInfoService = ytPlaceInfoService;
        this.ytContractInfoService = ytContractInfoService;
        this.ytPartnerService = ytPartnerService;
    }

    @GetMapping("/accounting")
    public AjaxResult accounting(CommonQuery commonQuery)
    {
        AjaxResult ajax = AjaxResult.success();
        int elevatorTotal = elevatorInfoService.getElevatorTotal();
        int elevatorBrandsTotal = elevatorBrandsService.getElevatorBrandsTotal();
        int signedCount =  ytPlaceInfoService.getSignedCount(commonQuery.getSignedStaute());
        int contractCount= ytContractInfoService.getContractStatusCount(commonQuery.getContractStatus());
        int partnerCount = ytPartnerService.getPartnerCount();
        List <Map> list = ytContractInfoService.getHeTongCount();
        // 合同統計
        Map hetongMap = new HashMap();
        for (Map map : list) {
            if ("终止".equals(map.get("status"))) {
                hetongMap.put("zhongzhi", ((Number) map.get("count")).intValue());
            } else if ("生效中".equals(map.get("status"))) {
                hetongMap.put("shengxiaozhong", ((Number) map.get("count")).intValue());
            } else if ("结束".equals(map.get("status"))) {
                hetongMap.put("jieshu", ((Number) map.get("count")).intValue());
            } else if ("签约中".equals(map.get("status"))) {
                hetongMap.put("qianyuezhong", ((Number) map.get("count")).intValue());
            }
        }
        ajax.put("heTongCount", hetongMap);

        ajax.put("signedCount", signedCount);
        ajax.put("elevatorTotal", elevatorTotal);
        ajax.put("elevatorBrandsTotal", elevatorBrandsTotal);
        ajax.put("contractCount", contractCount);
        ajax.put("partnerCount", partnerCount);



        return ajax;
    }


    @GetMapping("/getQuotation")
    public AjaxResult getQuotation()
    {
        AjaxResult ajax = AjaxResult.success();
         List<Map> list = new ArrayList<>();
        list = quotationSummaryService.selectLineQuotation();
        String[] xData = new String[list.size()];
        String[] expectedData = new String[list.size()];
        String[] actualData = new String[list.size()];

       for(int i=0;i<list.size();i++){
           xData[i] = list.get(i).get("month").toString();
           expectedData[i] = list.get(i).get("totalQuotation").toString();
           actualData[i] = list.get(i).get("totalPayment").toString();

       }

        ajax.put("xData",  xData);
        ajax.put("expectedData", expectedData);
        ajax.put("actualData",  actualData);
        return ajax;
    }

    @GetMapping("/getMaintenance")
    public AjaxResult getMaintenance()
    {
        AjaxResult ajax = AjaxResult.success();
        List<Map> list = new ArrayList<>();
        list = ytMaintenanceRecordService.selectLineMaintenance();
        String[] xDataT = new String[list.size()];
        String[] expectedDataT = new String[list.size()];
        String[] actualDataT = new String[list.size()];

        for(int i=0;i<list.size();i++){
            xDataT[i] = list.get(i).get("month").toString();
            expectedDataT[i] = list.get(i).get("daily_inspection_count").toString();
            actualDataT[i] = list.get(i).get("fault_repair_count").toString();

        }




        ajax.put("xData",  xDataT);
        ajax.put("expectedData", expectedDataT);
        ajax.put("actualData",  actualDataT);
        return ajax;
    }
}
